package com.example.myjavafx.controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.kordamp.ikonli.javafx.FontIcon;

import java.io.IOException;

public class LoginController {
    @FXML
    private TextField usernameField;
    @FXML
    private PasswordField passwordField;
    @FXML
    private CheckBox showPasswordCheckBox;
    @FXML
    private Label errorLabel;

    @FXML
    private void initialize() {

//        showPasswordCheckBox.selectedProperty().addListener((obs, wasSelected, isSelected) -> {
////            if (isSelected) {
//////                new FontIcon(AntDesignIconsOutlined.KEY)
////                passwordField.setEchoChar(null);
////            } else {
////                passwordField.setEchoChar('●');
////            }
////            passwordField.setCacheShape(isSelected ? '\0' : '●');
//        });
    }
    @FXML
    private void handleLogin() {
        String username = usernameField.getText().trim();
        String password = passwordField.getText().trim();

        // 简单验证逻辑（实际应加密并与数据库比对）
        if (username.isEmpty() || password.isEmpty()) {
            errorLabel.setText("用户名或密码不能为空！");
        } else if ("admin".equals(username) && "123456".equals(password)) {
            // 登录成功，跳转到主页面
            loadMainView();
        } else {
            errorLabel.setText("用户名或密码错误！");
        }
    }

    // 加载主页面并切换场景
    private void loadMainView() {
        try {
            // 加载主页面 FXML
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/example/myjavafx/main-view.fxml"));
            Parent root = loader.load();

            // 获取主页面控制器（可选，用于传递数据）
//            MainController mainController = loader.getController();
//            mainController.setUsername(usernameField.getText());
//            ModuleLayer.boot().addOpens("com.example.myjavafx.model", JavaFXModule.class.getModule());
            // 获取当前 Stage 并切换场景
            Stage stage = (Stage) usernameField.getScene().getWindow();
            stage.setScene(new Scene(root, 800, 600));
            stage.setTitle("主页面");
//            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}